package com.zhk.entry.scoket;


import com.zhk.entry.common.CommandTransf;
import com.zhk.entry.dao.impl.BaseDao;
import com.zhk.entry.mapper.UserMapperService;
import com.zhk.entry.mapper.impl.GoodsMapper;
import com.zhk.entry.mapper.impl.UserMapper;
import com.zhk.entry.model.Goods;
import com.zhk.entry.model.User;
import com.zhk.entry.observer.SprotSubject;
import com.zhk.entry.proxy.StuInvocationHandler;
import com.zhk.entry.util.IThreadConn;

import java.io.*;
import java.net.Socket;
import java.util.List;

public class ServerThread extends Thread {

    private Socket socket = null;

    private ObjectInputStream ois = null;

    private ObjectOutputStream oos = null;

    private IThreadConn iThreadConn;

    private UserMapperService userMapperService;


    public ServerThread(Socket socket, IThreadConn iThreadConn, UserMapperService userMapperService) {
        this.iThreadConn = iThreadConn;
        this.socket = socket;
        this.userMapperService = userMapperService;
    }


    @Override
    public void run() {
        try {
            InputStream is = socket.getInputStream();
            OutputStream os = socket.getOutputStream();
            ois = new ObjectInputStream(is);
            oos = new ObjectOutputStream(os);
            CommandTransf commandTransfer = (CommandTransf) ois.readObject();
            commandTransfer = excute(commandTransfer);//执行客户端指令操作
            oos.writeObject(commandTransfer);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * //     * 执行客户端指令操作
     * //     * @param commandTransfer
     * //     * @return
     * //
     */
    private CommandTransf excute(CommandTransf commandTransfer) {
        String cmd = commandTransfer.getCmd();
        if (cmd.equals("getUserAll")) {//查询全部
            User user = (User) commandTransfer.getData();
            List<User> list = userMapperService.getUser(user, iThreadConn.getConnection());
            commandTransfer.setData(list);
        }
        if (cmd.equals("saveUser")) {//新增

            //设置数据库
//            iThreadConn.setDataName("demo");
            //获取客户端传来的数据
            User user = (User) commandTransfer.getData();

            StuInvocationHandler<UserMapper> stuInvocationHandler = new StuInvocationHandler(new UserMapper(), iThreadConn);

            UserMapper service = stuInvocationHandler.createProxy();

            String msg = service.saveUser(user, iThreadConn.getConnection());

            //服务端返回的数据
            commandTransfer.setData(msg);
        }
        if (cmd.equals("saveGoodsAndUser")) {

            SprotSubject sprotSubject = new SprotSubject();

            try {
                //demo数据库
                User user = new User();
                user.setId(5);
                user.setName("Tom");
                user.setAge(24);
                user.setAddress("保利叶宇");

                UserMapper userMapper = new UserMapper();
                userMapper.setObserver("u1", sprotSubject, iThreadConn, userMapper);

                userMapper.saveUser(user, iThreadConn.getConnection());
                Goods goods = new Goods();
                goods.setGoodsName("菊乐酸奶");
                goods.setRepresent("酸奶");
                goods.setAddress("成都");

                GoodsMapper goodsMapper = new GoodsMapper();
                goodsMapper.setObserver("g1", sprotSubject, iThreadConn, goodsMapper);
                goodsMapper.saveGoods(goods, iThreadConn.getConnection());
//
//                //demo1数据库
                iThreadConn.setDataName("demo1");
                UserMapper userMapper1 = new UserMapper();
                userMapper1.setObserver("u2", sprotSubject, iThreadConn, userMapper1);
                userMapper1.saveUser(user, iThreadConn.getConnection());

                int i = 1 / 0;
                sprotSubject.addNews("commit");
            } catch (Exception e) {
                e.printStackTrace();
                sprotSubject.addNews("rollback");
                throw new RuntimeException(e);
            }
        }
        return commandTransfer;
    }
}
